package pers.kw.server.login.service;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import pers.kw.common.redis.service.RedisService;
import pers.kw.entity.TokenInfoDto;
import pers.kw.exceptions.BizException;
import pers.kw.feign.AuthApiService;
import pers.kw.protocol.R;
import pers.kw.server.login.dto.LoginReqDto;
import pers.kw.server.login.dto.RefreshTokenDto;

import javax.annotation.Resource;

@Slf4j
@Service
public class LoginService {

    @Resource
    private AuthApiService authApiService;

    @Resource
    private RedisService redisService;

    public TokenInfoDto login(LoginReqDto reqDto) {
        log.info("系统登录-获取token start...入参: {}", JSON.toJSONString(reqDto));
        R<TokenInfoDto> resp = authApiService.accessToken(
                "password", "web", reqDto.getUserNo(), reqDto.getPwd(),
                "1");
        if (!resp.isOk()) {
            throw new BizException(resp.getMsg());
        }
        log.info("系统登录-获取token end...出参: {}", JSON.toJSONString(resp.getBody()));
        return resp.getBody();
    }

    public TokenInfoDto refreshToken(RefreshTokenDto reqDto) {
        R<TokenInfoDto> resp = authApiService.refreshToken(
                "refresh_token", "web", reqDto.getUserNo(), "1",
                reqDto.getRefreshToken());
        if (!resp.isOk()) {
            throw new BizException(resp.getMsg());
        }
        return resp.getBody();
    }

    public void logout() {
        //登出接口，删除redis
        log.info("退出。。。");
    }
}
